package com.efast.cafe.portal.service.query.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RestController;

import com.efast.cafe.framework.bean.ResultBean;
import com.efast.cafe.framework.exception.ServiceException;
import com.efast.cafe.framework.util.QueryStringUtil;
import com.efast.cafe.framework.util.SpringWebUtils;
import com.efast.cafe.framework.util.SqlFormateUtil;
import com.efast.cafe.portal.bean.query.AbstractFieldType;
import com.efast.cafe.portal.bean.query.CommonTableConditionBean;
import com.efast.cafe.portal.bean.query.QueryBean;
import com.efast.cafe.portal.bean.query.QueryParameterBean;
import com.efast.cafe.portal.bean.query.TableConditionBean;
import com.efast.cafe.portal.bean.query.TableOpBean;
import com.efast.cafe.portal.bean.query.TemplateBean;
import com.efast.cafe.portal.bean.query.TemplateObjBean;
import com.efast.cafe.portal.dao.query.QueryTemplateDao;
import com.efast.cafe.portal.entity.query.TableView;
import com.efast.cafe.portal.entity.query.Template;
import com.efast.cafe.portal.entity.query.TemplateCondition;
import com.efast.cafe.portal.entity.query.TemplateJsonObj;
import com.efast.cafe.portal.entity.query.TemplateJsonObjQueryBean;
import com.efast.cafe.portal.entity.query.TemplateSave;
import com.efast.cafe.portal.service.query.IQueryTemplateService;
import com.efast.cafe.portal.service.query.IRedisResourceService;
import com.efast.cafe.util.ConfigUtil;
import com.efast.cafe.util.JsonUtil;
import com.efast.cafe.util.SetUtil;
import com.efast.cafe.util.StringUtil;
import com.github.pagehelper.PageHelper;


@Transactional
@RestController
public class QueryTmplateServiceImpl implements IQueryTemplateService {
	private final static byte[] lock = new byte[0]; 
	@Autowired
	private QueryTemplateDao queryTemplateDao;
	@Autowired
	private IRedisResourceService redisResourceService;
	
	/**
	 * 添加 模板
	 * @param template
	 * @param tableViews
	 * @param tableConditions
	 * @param contextPath
	 * @return
	 */
	@Override
	public Template addQueryTemplate(Template template, List<TableView> tableViews, List<TableConditionBean> tableConditions,String contextPath,Integer systemId) {
		template.setCompanyCode(SpringWebUtils.getRequestCompany());
		template.setCreateUser(SpringWebUtils.getRequestUser());
		if(StringUtil.isNullOrBlank(template.getCode())){
			Integer id = queryTemplateDao.selectTempalteMaxId();
			template.setCode(contextPath+template.getModule()+"-"+id);
		}else{
			Template t = queryTemplateDao.selectTemplateDetailByCode(template.getCode());
			if(t!=null){
				template.setId(t.getId());
				return updateQueryTemplate(template,tableViews,tableConditions,contextPath);
			}
		}
		template = addTemplate(template,ConfigUtil.getSystemIndentifier());
		TemplateObjBean objBean = new TemplateObjBean();
		if (template == null || template.getId() == null) {
			template = new Template();
			template.setId(0);
			return template;
		}
		
		int templateId = template.getId();
		addTableConditions(tableConditions, templateId);
		addTableViews(tableViews, templateId);
		for(TableConditionBean tableCondition:tableConditions){
			addTableOps(tableCondition.getTableOps(), templateId, tableCondition.getId());
			redisResourceService.updateTableOp(tableCondition.getTableOps(),tableCondition.getId());
		}
		template.setOptCounter(1);
		
		redisResourceService.updateTableCondition(tableConditions,templateId);
		if (!SetUtil.isEmpty(tableViews)) {
			redisResourceService.updateTableView(tableViews, templateId);
		}
		objBean.setTableConditions(tableConditions);
		objBean.setTemplate(template);
		objBean.setTableViews(tableViews);
		redisResourceService.updateTemplate(template,objBean);
		return template;
	}
	
	/**
	 * 给tableView tableCondition tableOp添加外键关联
	 * @param tables
	 * @param args
	 */
	/*private void addRefId(List<? extends TableBase> tables,int ... args){
		if(tables!=null&&tables.size()>0){
			Iterator<? extends TableBase> it = tables.iterator();
			TableBase obj = null;
			while(it.hasNext()){
					obj=it.next();
					if(args[0]!=0){
						obj.setId(args[0]);
					}
					args[0] += 2;
					if(args[1]!=0){
						obj.setQueryTemplateId(args[1]);
					}
					if(args[2]!=0){
						obj.setTableConditionId(args[2]);
					}
					obj.setIsDeleted(0);
			}
		}
	}*/
	
	/**
	 * 查询模板列表
	 * @param templateBean
	 * @return
	 */
	@Override
	public TemplateBean queryTemplateListByPage(TemplateBean templateBean) {
		// TODO Auto-generated method stub
		PageHelper.startPage(templateBean.getStart(),templateBean.getLength());
		templateBean.setResultList(queryTemplateDao.selectTemplateListByPage(templateBean));
		templateBean.setTotalRows(queryTemplateDao.selectTemplateListCount(templateBean));
		return templateBean;
	}
	
	/**
	 * 查询模板内容
	 * @param templateId
	 * @return
	 */
	@Override
	public TemplateObjBean queryTempalteDetail(Integer templateId,Integer templateSaveId) {
		// TODO Auto-generated method stub
		/*String tableName = "portal_query_template_key";
		SqlRowSetMetaData data = jdbcTemplate.queryForRowSet("select * from "+tableName+" limit 0").getMetaData();
		data.getColumnCount();
		for (int i = 1; i <= data.getColumnCount(); i++) {
			String type = data.getColumnTypeName(i);
			String columnName = data.getColumnName(i);
			System.out.print(columnName +"-"+type);
			System.out.print("\n");
		}*/
		return geTemplateObj(templateId, templateSaveId);
	}

	/**
	 * 更新模板
	 * @param template
	 * @param tableViews
	 * @param tableConditions
	 * @param contextPath
	 * @return
	 */
	@Override
	public Template updateQueryTemplate(Template template, List<TableView> tableViews, List<TableConditionBean> tableConditions,String contextPath) {
		// TODO Auto-generated method stub
		if (template == null) {
			template = new Template();
			template.setId(0);
			return template;
		}
			
		Integer templateId = template.getId();
		//template.setCode(contextPath+template.getModule()+"-"+templateId);
		Template selectedTemplate = queryTemplateDao.selectTemplateDetailById(templateId);
		if (selectedTemplate != null) {
			template.setOptCounter(selectedTemplate.getOptCounter());
		} else {
			template.setOptCounter(1);
		}
		queryTemplateDao.updateQueryTemplate(template);
		List<TableConditionBean> addList = new ArrayList<>();
		List<TableConditionBean> updateList = new ArrayList<>();
		if (!SetUtil.isEmpty(tableConditions)) {
			for (TableConditionBean tableCondition : tableConditions) {
				if (tableCondition.getId() != null) {
					updateList.add(tableCondition);
				} else {
					addList.add(tableCondition);
				}
			}
		}
		
		List<TableView> addLists = new ArrayList<>();
		List<TableView> updateLists = new ArrayList<>();
		if (!SetUtil.isEmpty(tableViews)) {
			for (TableView tableView : tableViews) {
				if (tableView.getId() != null) {
					updateLists.add(tableView);
				} else {
					addLists.add(tableView);
				}
			}
		}
		
		queryTemplateDao.deleteTableCondition(updateList,templateId);
		if (!SetUtil.isEmpty(updateList)) {
			queryTemplateDao.updateTableCondition(updateList);
		}
		queryTemplateDao.deleteTableView(updateLists,templateId);
		if (!SetUtil.isEmpty(updateLists)) {
			//queryTemplateDao.updateTableView(updateLists);
			addTableViews(updateLists, templateId);
		}
		addTableViews(addLists, templateId);
		addTableConditions(addList, templateId);
		if (!SetUtil.isEmpty(updateLists) || !SetUtil.isEmpty(addLists))  {
			redisResourceService.updateTableView(SetUtil.combine(addLists,updateLists),templateId);
		}
		if (!SetUtil.isEmpty(updateList) || !SetUtil.isEmpty(addList))  {
			redisResourceService.updateTableCondition(SetUtil.combine(addList,updateList),templateId);
		}
		
		if (!SetUtil.isEmpty(tableConditions)) {
			for(TableConditionBean tableCondition:tableConditions){
				List<TableOpBean> add = new ArrayList<>();
				List<TableOpBean> update = new ArrayList<>();
				if (!SetUtil.isEmpty(tableCondition.getTableOps())) {
					for (TableOpBean tableOp : tableCondition.getTableOps()) {
						if (tableOp.getId() != null) {
							tableOp.setTableConditionId(tableCondition.getId());
							update.add(tableOp);
						} else {
							add.add(tableOp);
						}
					}
				}
				
				queryTemplateDao.deleteTableOp(update,tableCondition.getId());
				if (!SetUtil.isEmpty(update)) {
					queryTemplateDao.updateTableOp(update);
				} else {
					queryTemplateDao.deleteTableOpByTableConditionId(tableCondition.getId());
				}
				addTableOps(add, templateId, tableCondition.getId());
				if (!SetUtil.isEmpty(update) || !SetUtil.isEmpty(add))  {
					redisResourceService.updateTableOp(SetUtil.combine(add,update),tableCondition.getId());
				}
				
			}
		}
		TemplateObjBean objBean = new TemplateObjBean();
		objBean.setTableConditions(tableConditions);
		objBean.setTemplate(template);
		objBean.setTableViews(tableViews);
		redisResourceService.updateTemplate(template,objBean);
		return template;
	}
	
	/**
	 * 删除模板
	 * @param templateId
	 * @return
	 */
	@Override
	public int deleteQueryTemplate(Integer templateId) {
		Template selectedTemplate = queryTemplateDao.selectTemplateDetailById(templateId);
		selectedTemplate.setIsDelete(1);
		selectedTemplate.setCompanyCode(SpringWebUtils.getRequestCompany());
		redisResourceService.updateTemplate(selectedTemplate,null);
		return queryTemplateDao.deleteQueryTemplate(selectedTemplate);
	}
	
	/**
	 * 添加保存查询
	 * @param templateSave
	 * @return
	 */
	@Override
	public int addTemplateSave(TemplateJsonObj templateSave) {
		templateSave.setCompanyCode(SpringWebUtils.getRequestCompany());
		templateSave.setCreateUser(SpringWebUtils.getRequestUser());
		return addTemplateSave(templateSave, false);
	}
	
	/**
	 * 更新保存查询
	 * @param templateSave
	 * @return
	 */
	@Override
	public int updateTemplateSave(TemplateJsonObj templateSave) {
		templateSave.setCompanyCode(SpringWebUtils.getRequestCompany());
		templateSave.setCreateUser(SpringWebUtils.getRequestUser());
		return addTemplateSave(templateSave, true);
	}
	
	
	public int addTemplateSave(TemplateJsonObj save,Boolean isUpdate) {
		synchronized(lock){
			try {
			if (save == null || save.getQueryTemplateId() == null) {
				return 0;
			}
			Integer templateId = save.getQueryTemplateId();
			Integer status = 0;
			if(!isUpdate){
				TemplateSave templateSave = new TemplateSave();
				Integer templateSaveId = queryTemplateDao.selectTemplateSaveMaxId();
				List<TableConditionBean> tableConditions = redisResourceService.queryTableConditionBean(save.getQueryTemplateId());//查询当前关联的tableCondition
				List<TemplateCondition> addList = new ArrayList<>();
				addList = addTemplateCondition(tableConditions, templateId, templateSaveId);
				String tableConditionRef;
			    tableConditionRef = JsonUtil.toJson(addList);
				templateSave.setConditions(addList);
				templateSave.setCode(save.getCode());
				templateSave.setQueryTemplateId(templateId);
				templateSave.setTableConditionJson(save.getConditionJson());
				templateSave.setTableConditionRef(tableConditionRef);
				save.setTableConditionRef(tableConditionRef);
				save.setId(templateSaveId);
				status = queryTemplateDao.addTemplateSave(save);
				redisResourceService.updateTemplateSave(templateSave,templateId);
			}
			return status;
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return 0;
		}
		
		
	}
	/**
	 * 添加模板内容
	 * @param template
	 * @return
	 */
	private Template addTemplate(Template template,String systemId) {
		synchronized(lock){
			if (template == null) {
				return template;
			}
			if(template.getUuid() == null){
				template.setUuid(UUID.randomUUID().toString());
			}
			if(template.getId()==null){
				//新增时候，判断在系统下面name唯一
				int count = queryTemplateDao.selectTemplateCountByName(systemId, template.getName());
				if(count!=0){
					throw new ServiceException("系统下template应该唯一，请更新改name字段");
				}
			}
			
			int templateId=queryTemplateDao.selectTempalteMaxId();
			template.setId(templateId);
			template.setIsDelete(0);
			template.setOptCounter(1);
			queryTemplateDao.addQueryTemplate(template);//增加查询模板
			return template;
		}
	}
	/**
	 * 添加tableCondition
	 * @param list
	 * @param templateId
	 * @return
	 */
	private List<TableConditionBean> addTableConditions(List<TableConditionBean> list,Integer templateId) {
		synchronized(lock){
			if (SetUtil.isEmpty(list) || templateId == null) {
				return list;
			}
			int tableConditionId=queryTemplateDao.selectTableConditionMaxId();
			/*addRefId(list,tableConditionId,templateId,0);*/
			for (TableConditionBean tableConditionBean : list) {
				tableConditionBean.setId(tableConditionId);
				tableConditionBean.setQueryTemplateId(templateId);
				tableConditionBean.setIsDeleted(0);
				tableConditionId +=2;
			}
			queryTemplateDao.addTableCondition(list);//增加条件视图
			return list;
		}
	}
	
	/**
	 * 添加tableView
	 * @param list
	 * @param templateId
	 */
	private void addTableViews(List<TableView> list,Integer templateId) {
		synchronized(lock){
			if (SetUtil.isEmpty(list) || templateId == null) {
				return;
			}
			int tableViewId = queryTemplateDao.selectTableViewMaxId();
			/*addRefId(list,tableViewId,templateId, 0);*/
			for (TableView tableView : list) {
				tableView.setId(tableViewId);
				tableView.setQueryTemplateId(templateId);
				tableView.setIsDeleted(0);
				tableViewId +=2;
			}
			queryTemplateDao.addTableView(list);//增加字段视图
		}
	}
	
	/**
	 * 添加tableOP
	 * @param list
	 * @param templateId
	 * @param tableConditionId
	 */
	private void addTableOps(List<TableOpBean> list,Integer templateId,Integer tableConditionId) {
		if (SetUtil.isEmpty(list) || templateId == null || tableConditionId == null) {
			return;
		}
		/*addRefId(list,0,templateId,tableConditionId);*/
		for (TableOpBean tableOpBean : list) {
			tableOpBean.setQueryTemplateId(templateId);
			tableOpBean.setTableConditionId(tableConditionId);
			tableOpBean.setIsDeleted(0);
		}
		queryTemplateDao.addTableOp(list);
	}
	
	/**
	 * 删除保存查询
	 * @param id
	 * @return
	 */
	@Override
	public int deleteTemplateSave(Integer id) {
		//queryTemplateDao.deleteTemplateConditionDetail(id);
		redisResourceService.updateTemplateSave(null, id);
		return queryTemplateDao.deleteTemplateSave(id);
		//queryTemplateDao.deleteTemplateKey(id);
		//return queryTemplateDao.deleteTemplateSave(id);
	}
	
	/**
	 * 查询保存查询列表
	 * @param queryBean
	 * @return
	 */
	@Override
	public ResultBean queryTemplateSaveBeanList(QueryBean queryBean) {
		ResultBean bean = new ResultBean();
		try {
			bean.setObject(queryTemplateDao.queryTemplateSaveList(queryBean));
			bean.setSuccess(true);
		} catch (Exception e) {
			bean.setMessage(e.getMessage());
		}
		
		return bean;
	}
	
	
	/**
	 * 添加 保存查询where条件 templateCondition
	 * @param tableConditions
	 * @param queryTemplateId
	 * @param templateSaveId
	 * @return
	 */
//	private List<TemplateCondition> addTemplateCondition(List<TableConditionBean> tableConditions,Integer queryTemplateId,Integer templateSaveId) {
//		List<TemplateCondition> addList = new ArrayList<>();
//		if (!SetUtil.isEmpty(tableConditions)) {
//			for (TableConditionBean tableConditionBean : tableConditions) {
//				TemplateCondition condition = new TemplateCondition();
//				condition.setQueryTemplateId(queryTemplateId);
//				condition.setTableConditionId(tableConditionBean.getId());
//				condition.setTemplateSaveId(templateSaveId);
//				addList.add(condition);
//			}
//			
//		} else {
//			TemplateCondition condition = new TemplateCondition();
//			condition.setQueryTemplateId(queryTemplateId);
//			condition.setTemplateSaveId(templateSaveId);
//			addList.add(condition);
//		}
//		redisResourceService.updateTemplateCondition(addList, templateSaveId);
//		return addList;
//	}

	@Override
	/**
	 * 根据配置生成sql
	 * @param querys
	 * @return
	 */
	public ResultBean generateSqlByTemplate(QueryBean querys) {
		if (querys == null) {
			return null;
		}
		ResultBean bean = new ResultBean();
		try {
			StringBuffer buffer = new StringBuffer();
			Template template= querys.getTemplate();
			if (template != null) {
				buffer.append(template.getSelectArea())
				.append(" ")
				.append(template.getFromArea())
				.append(" ")
				.append(handelTableCondition(template.getWhereArea(),querys.getTableConditions()))
				.append(" ")
				.append(QueryStringUtil.getNonEmpty(template.getGroupbyArea()))
				.append(" ")
				.append(QueryStringUtil.getNonEmpty(template.getOrderbyArea()));
				String sql = SqlFormateUtil.format(buffer.toString());
				bean.setObject(sql);
				bean.setSuccess(true);
			}
		} catch (Exception e) {
			bean.setMessage(e.getMessage());
		}
		
		return bean;
	}
	
	@Override
	/**
	 * 更据模板Code 获得查询结果
	 * @param code
	 * @return
	 */
	public QueryBean queryTempalteDetailByCode(String code,Integer templateSaveId) {
		return covertQueryBean(code,templateSaveId);
	}
	
	private QueryBean covertQueryBean(String code,Integer templateSaveId) {
//		String id = "0";
//		String [] arrays = code.split("-");
//		if (arrays.length > 1) {
//			id = arrays[1];
//		}
//		Integer templateId = null;
//		try {
//			templateId = Integer.parseInt(id);
//		} catch (Exception e) {
//			return null;
//		}
//		
		Template t = queryTemplateDao.selectTemplateDetailByCode(code);
		
		return covertQueryBean(t.getId(), templateSaveId);
	}
	
	/**
	 * 根据模板组装QueryBean（模板的查询结果）
	 * @param template
	 * @return
	 */
	private QueryBean covertQueryBean(Integer templateId,Integer templateSaveId) {
		QueryBean queryBean = new QueryBean();
		List<TableConditionBean> tableConditions = null;
		if (templateId != null && templateId != 0) {
			TemplateObjBean objBean = redisResourceService.geTemplateObjBean(templateId);
			if (templateSaveId != null) {
				List<TemplateCondition> list = redisResourceService.queryTemplateSaveBean(templateSaveId).getConditions();
				if (!SetUtil.isEmpty(list)) {
					tableConditions = new ArrayList<>();
					for (TemplateCondition templateCondition : list) {
						TableConditionBean bean = queryTemplateDao.selecetTableConditionListById(templateCondition.getTableConditionId());
						if (QueryStringUtil.DFT_NUMBER.equals(bean.getIsDeleted().toString())) {
							tableConditions.add(bean);
						} else {
							queryBean.setSuccess(false);
							queryBean.setMessage(SqlFormateUtil.DELETED_MESSAGE);
							return queryBean;
						}
						
					}
				}
			} else {
				tableConditions = objBean.getTableConditions();
			}
			if (!SetUtil.isEmpty(tableConditions)) {
				for(TableConditionBean tableCondition:tableConditions){
					Integer tableConditionId=tableCondition.getId();
					List<TableOpBean> tableOps = redisResourceService.queryTableOp(tableConditionId);
					tableCondition.setTableOps(tableOps);
				}
			}
			queryBean.setTemplate(objBean.getTemplate());
			queryBean.setTableConditions(tableConditions);
			queryBean.setTableViews(objBean.getTableViews());
			queryBean.setStart(0);
			queryBean.setLength(10);
			queryBean.setSuccess(true);
		}
		return queryBean;
	}
	/**
	 * preview tableConditio处理
	 * @param whereArea
	 * @param conditions
	 * @return
	 */
	private String handelTableCondition(String whereArea,List<TableConditionBean> conditions) {
		if (SetUtil.isEmpty(conditions)) {
			return whereArea;
		}
		Boolean isFirstAppend = true;
		if (QueryStringUtil.isEmpty(whereArea)) {
			whereArea = " WHERE ";
			isFirstAppend = false;
		}
		StringBuffer buffer = new StringBuffer();
		buffer.append(whereArea);
		for (TableConditionBean bean : conditions) {
			if (isFirstAppend) {
				buffer.append(" AND ");
			}
			isFirstAppend = true;
			QueryParameterBean param = new QueryParameterBean();
			CommonTableConditionBean conditionBean = AbstractFieldType.handelCondition(param,bean,true);
			buffer.append(conditionBean.getBindSql());
			buffer.append(" ");
		}
		return buffer.toString();
	}
	
	
	private TemplateObjBean geTemplateObj(Integer templateId,Integer templateSaveId) {
		TemplateObjBean obj = new TemplateObjBean();
		List<TableConditionBean> tableConditions = null;
		if (templateId != null && templateId != 0) {
			TemplateObjBean objBean = redisResourceService.geTemplateObjBean(templateId);
			if (templateSaveId != null) {
				List<TemplateCondition> list = redisResourceService.queryTemplateSaveBean(templateSaveId).getConditions();
				if (!SetUtil.isEmpty(list)) {
					tableConditions = new ArrayList<>();
					for (TemplateCondition templateCondition : list) {
						TableConditionBean bean = queryTemplateDao.selecetTableConditionListById(templateCondition.getTableConditionId());
						if (QueryStringUtil.DFT_NUMBER.equals(bean.getIsDeleted().toString())) {
							tableConditions.add(bean);
						} else {
							obj.setSuccess(false);
							obj.setMessage(SqlFormateUtil.DELETED_MESSAGE);
							return obj;
						}
						
					}
				}
			} else {
				tableConditions = objBean.getTableConditions();
			}
			if (!SetUtil.isEmpty(tableConditions)) {
				for(TableConditionBean tableCondition:tableConditions){
					Integer tableConditionId=tableCondition.getId();
					List<TableOpBean> tableOps = redisResourceService.queryTableOp(tableConditionId);
					tableCondition.setTableOps(tableOps);
				}
			}
			obj.setTemplate(objBean.getTemplate());
			obj.setTableConditions(tableConditions);
			obj.setTableViews(objBean.getTableViews());
			obj.setSuccess(true);
		}
		
		return obj;
	}
	
	/**
	 * 添加 保存查询where条件 templateCondition
	 * @param tableConditions
	 * @param queryTemplateId
	 * @param templateSaveId
	 * @return
	 */
	private List<TemplateCondition> addTemplateCondition(List<TableConditionBean> tableConditions,Integer queryTemplateId,Integer templateSaveId) {
		List<TemplateCondition> addList = new ArrayList<>();
		if (!SetUtil.isEmpty(tableConditions)) {
			for (TableConditionBean tableConditionBean : tableConditions) {
				TemplateCondition condition = new TemplateCondition();
				condition.setQueryTemplateId(queryTemplateId);
				condition.setTableConditionId(tableConditionBean.getId());
				condition.setTemplateSaveId(templateSaveId);
				addList.add(condition);
			}
			
		} else {
			TemplateCondition condition = new TemplateCondition();
			condition.setQueryTemplateId(queryTemplateId);
			condition.setTemplateSaveId(templateSaveId);
			addList.add(condition);
		}
		return addList;
	}

	/**
	 * 查询模块关联的module
	 * {@inheritDoc} 
	 * overridden:
	 * @Date        :      2017年9月20日
	 * @see org.eae.eldf.portal.remote.query.QueryTemplateService#selectTemplateListByMouduleId(java.lang.Integer)
	*
	 */
	@Override
	public List<TemplateBean> selectTemplateListByMouduleId(Integer id,String code,String i18nName) {
		//PageHelper.startPage(pageNum, pageSize)
		return queryTemplateDao.selectTemplateListByMouduleId(id,code,i18nName);
	}

	/**
	 * 查询条件
	 * {@inheritDoc} 
	 * overridden:
	 * @Date        :      2017年9月25日
	 * @see org.eae.eldf.portal.remote.query.QueryTemplateService#queryTemplateSaveListAll(org.eae.eldf.portal.bean.query.QueryBean)
	*
	 */
	@Override
	public ResultBean queryTemplateSaveListAll(QueryBean queryBean) {
		ResultBean result = new ResultBean(true,null);
		queryBean.setVersion(ConfigUtil.getSystemVersion());
		queryBean.setIndentifier(ConfigUtil.getSystemIndentifier());
		try {
			TemplateJsonObjQueryBean bean = new TemplateJsonObjQueryBean();
			bean.setResultList(queryTemplateDao.queryTemplateSaveListByLoginUser(queryBean));
			bean.setCount(queryTemplateDao.queryTemplateSaveCount(queryBean));
			result.setObject(bean);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			result.setSuccess(false);
			e.printStackTrace();
		}
		return result;
	}

	@Override
	public void updateFormLayout(TemplateBean template) {
		// TODO Auto-generated method stub
		queryTemplateDao.updateFormLayout(template);
		TemplateObjBean templateObjBean= redisResourceService.geTemplateObjBean(template.getId());
		redisResourceService.updateTemplateLayoutBean(templateObjBean,template.getFormLayout(), template.getId());
	}

	
	    /* (非 Javadoc)
	    * 
	    * 
	    * @return
	    * @see com.efast.cafe.portal.service.query.IQueryTemplateService#queryModuleList()
	    */
	    
	@Override
	public ResultBean queryModuleList() {
		ResultBean bean = new ResultBean();
		try {
			bean.setObject(queryTemplateDao.queryModuleList());
			bean.setSuccess(true);
		} catch (Exception e) {
			bean.setMessage(e.getMessage());
		}
		return bean;
	}

}
